// [[ OnflowGenerator File UUID: 72d7638ae3184f6095458887c0a5a075  ]]
package com.itdct.onflow.system.component.service

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.itdct.onflow.core.constant.CommonString
import com.itdct.onflow.core.constant.ReturnCode
import com.itdct.onflow.core.extend.*
import com.itdct.onflow.core.vo.KeyLabelValueVo
import com.itdct.onflow.core.vo.RespPageVo
import com.itdct.onflow.core.vo.RespVo
import com.itdct.onflow.system.common.base.BaseSysService
import com.itdct.onflow.system.common.context.Context
import com.itdct.onflow.system.common.extend.dict
import com.itdct.onflow.system.component.entity.SysComponentEntity
import com.itdct.onflow.system.component.mapper.SysComponentMapper
import com.itdct.onflow.system.component.qo.SysComponentEditQo
import com.itdct.onflow.system.component.qo.SysComponentSearchQo
import com.itdct.onflow.system.component.vo.SysComponentDetailVo
import com.itdct.onflow.system.component.vo.SysComponentListVo
import com.itdct.onflow.system.role.entity.SysRoleComponentEntity
import com.itdct.onflow.system.role.mapper.SysRoleComponentMapper
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * @author DCTANT
 * @date 2025-09-15 16:13:06
 * @version 1.0
 * @description 系统组件服务
 */
@Service
class SysComponentService(
    val sysComponentMapper: SysComponentMapper,
    val sysRoleComponentMapper: SysRoleComponentMapper
) : BaseSysService() {

    @Transactional(rollbackFor = [Exception::class])
    fun add(qo: SysComponentEditQo, context: Context): RespVo<Long> {
        val addEntity = qo.toEntity(SysComponentEntity::class.java)
        val id = sysComponentMapper.insertEx(addEntity)
        return success(id)
    }

    @Transactional(rollbackFor = [Exception::class])
    fun update(qo: SysComponentEditQo, context: Context): RespVo<Unit> {
        val updateEntity = sysComponentMapper.selectByIdEx(qo.id)
        qo.injectValue(updateEntity)
        sysComponentMapper.updateEx(updateEntity)
        return success()
    }

    fun delete(qo: SysComponentEditQo, context: Context): RespVo<Unit> {
        val ids = qo.ids ?: fail(ReturnCode.LACK_PARAM, CommonString.REQUIRE_DELETE_ID_LIST)
        val deleteEntities = sysComponentMapper.selectByIdsEx(ids)
        sysComponentMapper.deleteBatchEx(deleteEntities)
        return success()
    }

    fun detail(qo: SysComponentSearchQo, context: Context): RespVo<SysComponentDetailVo> {
        val id = qo.id ?: fail(ReturnCode.LACK_PARAM, CommonString.REQUIRE_ID)
        val vo = sysComponentMapper.findById(id) ?: fail(CommonString.ENTITY_NOT_FOUND_USAGE("系统组件", id))
        return success(vo.dict())
    }

    fun page(qo: SysComponentSearchQo, context: Context): RespPageVo<SysComponentListVo> {
        startPage(context)
        val pageData = sysComponentMapper.findByList(qo)
        return successPage(pageData.dict())
    }

    fun getByMenuId(qo: SysComponentSearchQo, context: Context): RespVo<List<KeyLabelValueVo>> {
        val pageData = sysComponentMapper.findByList(qo)

        val menuId = qo.menuId
        val queryWrapper = KtQueryWrapper(SysRoleComponentEntity::class.java)
        queryWrapper.eq(SysRoleComponentEntity::menuId, menuId)
        val componentPermissionRoleRelEntities = sysRoleComponentMapper.selectList(queryWrapper)
        val componentIdMap = componentPermissionRoleRelEntities.associateBy { it.componentId }

        val voList = pageData.map {
            val keyLabelValueVo = KeyLabelValueVo()
            keyLabelValueVo.key = it.id.toString()
            keyLabelValueVo.label = it.title
            val componentPermissionRoleRelEntity = componentIdMap[it.id]
            if (componentPermissionRoleRelEntity != null) {
                keyLabelValueVo.value = true
            } else {
                keyLabelValueVo.value = false
            }
            keyLabelValueVo
        }
        return success(voList)
    }

// [[ OnflowGenerator Range (- selectFunctions -) Start ]]

    fun selectTitleList(qo: SysComponentSearchQo, context: Context): RespVo<List<KeyLabelValueVo>> {
        val vos = sysComponentMapper.selectTitleList(qo)
        return success(vos)
    }
// [[ OnflowGenerator Range (- selectFunctions -) End ]]
}